package com.renrendoc.network.flow.source;

import com.renrendoc.network.flow.beans.NginxLogEvent;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.util.Date;
import java.util.HashMap;
import java.util.Random;
import java.util.UUID;

public class ClickSource implements SourceFunction<NginxLogEvent> {

    boolean running = true;

    @Override
    public void run(SourceContext<NginxLogEvent> ctx) throws Exception {
        // 定义一个随机数发生器
        Random random = new Random();

        String site = "https://www.renrendoc.com/paper/{aa}.html";
        String[] user = {"lily", "tom", "lucy", "jim"};
        String[] url = {"/doc?a=3", "233.html", "html/abc?g=1", "/c.php"};
//        String [] eta = {"页面", "点击", "自定义"};
        String [] eta = {"页面"};
        String [] page = {"首页", "详情页", "分类页"};
        String[] sk = {"6a5c78ee0a40875a43252c84c5625146", "7a5c78ee0a40875a43252c84c5625146", "8a5c78ee0a40875a43252c84c5625146"};


        while (running){
// 生成随机索引
            HashMap<String, String> m = new HashMap<>();
            String skey = sk[random.nextInt(sk.length)];
            String si = generateRandomString();
            String ci = generateRandomString();
            String u = site.replace("{aa}", random.nextInt(200000000) + "");
            String ru = site.replace("{aa}", random.nextInt(200000000) + "");
            String et = eta[random.nextInt(eta.length)];
            String tt = generateRandomString();
            String v = "1.0.0";
            String ep = page[random.nextInt(page.length)];
//            String ep = 1 + "";
            String cs = "{}";
            String wh = "1024x768";
            String rnd = random.nextInt(2000000) + "";
            String ip = generateRandomString();
            long ts = System.currentTimeMillis();
            NginxLogEvent nginx = new NginxLogEvent(
                    skey, si, ci, u, ru, et, tt, v, ep, cs, wh, rnd, ts
            );
//            nginx.ua = "xx baiduspider";

            ctx.collect(nginx);

            // 控制输出频率
            Thread.sleep(1000L);
        }
    }

    @Override
    public void cancel() {
        running = false;
    }

    public static String generateRandomString() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replace("-", "");
    }
}
